<?php

namespace app\common\model;

use think\Model;

class Comment extends Model
{
    protected $autoWriteTimestamp = true;
    // 关联用户表
    public function user()
    {
        return $this->belongsTo('user');
    }
    // 评论的方法
    public function comment()
    {
        $param = request()->param();
        // 获取用户的id
        $userId = request()->userId;
        $comment = self::create([
            'user_id' => $userId,
            'post_id' => $param['post_id'],
            'fid' => $param['fid'],
            'data' => $param['data']
        ]);
        if($comment)
        {
            if($param['fid'] > 0)
            {
                $fcomment =  self::get($param['fid']);
                $fcomment->fnum = ['inc',1];
                $fcomment->save();
                
            }
            return true;
        }
        TApiException('评论失败');
    }
}
